module.exports = class extends baseLogic {
    constructor() {
        super();
    }

    static init() {
        return new this();
    }

    async getItems() {
        return await this.DB.iframe_system_module.Access.findAll({
            order: [
                ['Sort', 'ASC']
            ],
            raw: true
        });
    }

    async insertOrUpdate(body){
        let ret={};
        let ModuleID = body.ModuleID ? body.ModuleID : 0;
        let parentID = body.ParentID ? body.ParentID : 0;
        let UrlGoModuleID = Number(body.UrlGoModuleID);
        if (UrlGoModuleID > 0 || UrlGoModuleID == -1) {
            body.URL = "";
        }
        if (body.URL && body.URL !== "" && body.URL.toLowerCase() !== "javascript:;") {
            let count = await this.DB.iframe_system_module.Access.count({
                where: {
                    URL: body.URL,
                    ModuleID: {
                        '$ne': ModuleID,
                    }
                }
            });
            if (count > 0) {
                return null;
            }
        }
        let Model = {};
            if (ModuleID > 0) {
                Model = await this.DB.iframe_system_module.Access.findById(ModuleID);
                if (!Model) {
                    console.log('系统模块编辑-记录不存在');
                    return null;
                }
                Model = Model.dataValues;
            } else {
                let MaxSort = await this.DB.iframe_system_module.Access.max("Sort", {
                    where: {
                        ParentID: parentID
                    }
                });
                if (isNaN(MaxSort)) {
                    MaxSort = 0;
                }
                Model.IsParent = 0;
                Model.ParentID = parentID;
                Model.Sort = Number("0" + MaxSort) + 1;
                Model.OS_CreateTime = this.moment().format("YYYY-MM-DD HH:mm:ss");
            }
            Model.Name = body.Name;
            Model.URL = body.URL;
            Model.UrlGoModuleID = body.UrlGoModuleID;
            Model.Able = body.Able;
            Model.Remark = body.Remark;
            Model.Icon = body.Icon;
            Model.IsShowInMenu = Number("0" + body.IsShowInMenu);
            Model.IsShowLeftMenu = Number("0" + body.IsShowLeftMenu);
            Model.DataOptions=body.DataOptions;
            if (ModuleID > 0) {
                 await this.DB.iframe_system_module.Access.update(Model, {
                    where: {
                        ModuleID: ModuleID
                    }
                });
            } else {
                Model.ModuleID = await this.DB.iframe_tabelid.GetID("iframe_system_module");
                Model= await this.DB.iframe_system_module.Access.create(Model);
            }
            await this.CheckIsParent_SystemModule(Model.ParentID);
            return Model;
    }

    async delModule(keys,CurNodeParentID){
        for (let i = 0; i < keys.length; i++) {
            let id = Number("0" + keys[i]);
            if (id > 0) {
                await this.DB.iframe_system_module.Access.destroy({
                    where: {
                        ModuleID: id
                    }
                });
            }
        }
        await this.CheckIsParent_SystemModule(CurNodeParentID);
    }

    async moveNode(queryModel){
        let ret={};
        let TargetNodeChildrensIDs_array = queryModel.TargetNodeChildrensIDs.split(',');
        if (queryModel.CurNodeID > 0) {
            await this.DB.iframe_system_module.Access.update({
                ParentID: queryModel.TargetParentID
            }, {
                where: {
                    ModuleID: queryModel.CurNodeID
                }
            });
        };
        for (let i = 0; i < TargetNodeChildrensIDs_array.length; i++) {
            let id = TargetNodeChildrensIDs_array[i];
            await this.DB.iframe_system_module.Access.update({
                Sort: i
            }, {
                where: {
                    ModuleID: id
                }
            });
        };
        await this.CheckIsParent_SystemModule(queryModel.TargetParentID);
        await this.CheckIsParent_SystemModule(queryModel.old_parent);
        return true;
    }

    async publish(){
        let fs = require("fs");
        let ret = await this.func.systemModule.Sync();
        let outFiltPath = __dirname + "/../../www/res/system-module-bak/" + this.moment().format('YYYYMMDDHHmmss') + ".json";
        let fileContent = JSON.stringify(ret);
       await fs.writeFileSync(outFiltPath, fileContent, 'utf8');
        return true;
    }

    async getJsonFile(){
        let fs = require("fs");
        let JsonFilePath = __dirname + "/../../www/res/system-module-bak/";
        let files = fs.readdirSync(JsonFilePath);
        return files;
    }

    async rollBack(fileName){
        let fs = require("fs");
        let JsonFilePath = __dirname + "/../../www/res/system-module-bak/" + fileName;
        let fileContent = fs.readFileSync(JsonFilePath, 'utf-8');
        let Modules = JSON.parse(fileContent);
        await DB.iframe_system_module.Access.destroy({
            // limit: count,
            where: {}
        });
        for (let i = 0; i < Modules.length; i++) {
            await this.DB.iframe_system_module.Access.create(Modules[i]);
        }
        return true;
    }

    async CheckIsParent_SystemModule(ParentID){
        if (ParentID === 0) {
            return false;
        };
        let count = await this.DB.iframe_system_module.Access.count({
            where: {
                ParentID: ParentID
            }
        });
        if (count == 0) {
            await this.DB.iframe_system_module.Access.update({
                IsParent: 0
            }, {
                where: {
                    ModuleID: ParentID
                }
            });
        } else {
            await this.DB.iframe_system_module.Access.update({
                IsParent: 1
            }, {
                where: {
                    ModuleID: ParentID
                }
            });
        }
        return true;
    }
}
